<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            margin: 100px auto;
            border-radius: 50%
        }
    </style>
</head>

<body>
<script>
    // console.log(parseInt(Math.random()*10));
    function getColor() {
        var str = "#";
        var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
        for (var i = 0; i < 6; i++) {
            //产生的每个随机数都是索引。
            var num = parseInt(Math.random() * 16)
            str += arr[num];
        }
        return str;
    }

    window.onload = function () {
        document.getElementById("dv").style.backgroundColor = getColor();


    }
</script>
<div id="dv"></div>

</body>

</html>